'use strict';

var rq = require('request-promise'),
  debug = require('debug'),
  log = debug('yozh:lib:log'),
  error = debug('yozh:lib:error'),
  verbose = debug('yozh:lib:verbose'),
  config = require('./config'),
  tools = require('../lib/tools'),
  hmac = require('../lib/hmac')
  ;

var httpRequest = module.exports = {};

httpRequest.sendCodes = function(to, codes) {
  var timestamp = tools.getCodesTimestamp();
  var sig = hmac.encrypt(config.md5, null, config.codesConf.sid +
    config.codesConf.token + timestamp);
  var options = {
    method: 'POST',
    uri: config.codesConf.url +
      '/' + config.codesConf.softVersion +
      '/accounts/' + config.codesConf.sid +
      config.codesConf.emailSmsUrl,
    body: {
      appId: config.codesConf.appId,
      emailTemplateId: config.codesConf.emailTemplateId,
      to: to,
      param: codes,
      sig: sig,
      timestamp: timestamp
    }
  }
  return rq(options).promise();
};
